package cn.admobiletop.adsuyi.adapter.gdt.manager;

import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.admobiletop.adsuyi.ad.ADSuyiAd;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiReleaseListener;
import cn.admobiletop.adsuyi.adapter.gdt.widget.CustomizeMediaView;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 视频视图缓存管理器
 * @date 2019/8/15
 */
public class MediaViewCacheManager {
    private static MediaViewCacheManager instance;
    private Map<String, List<CustomizeMediaView>> mediaViewViewCacheMap = new HashMap<>();

    private MediaViewCacheManager() {
    }

    public static MediaViewCacheManager getInstance() {
        if (instance == null) {
            synchronized (MediaViewCacheManager.class) {
                if (instance == null) {
                    instance = new MediaViewCacheManager();
                }
            }
        }
        return instance;
    }

    /**
     * 获取视频控件
     *
     * @param container 容器
     * @param adSuyiAd  广告对象
     * @return
     */
    public synchronized CustomizeMediaView getCacheMediaView(ViewGroup container, ADSuyiAd adSuyiAd) {
        if (ADSuyiAdUtil.isReleased(adSuyiAd) || container == null) {
            return null;
        }
        String key = adSuyiAd.getKey();
        List<CustomizeMediaView> customizeMediaViewList = mediaViewViewCacheMap.get(key);
        if (customizeMediaViewList == null) {
            customizeMediaViewList = new ArrayList<>();
            mediaViewViewCacheMap.put(key, customizeMediaViewList);
            // 注册ADSuyiAd释放监听
            adSuyiAd.addReleaseListener(new ADSuyiReleaseListener() {
                @Override
                public void onRelease(ADSuyiAd adSuyiAd) {
                    if (adSuyiAd != null) {
                        release(adSuyiAd.getKey());
                    }
                }
            });
        }

        CustomizeMediaView idleCustomizeMediaView = null;
        long tempIdleTime = System.currentTimeMillis();
        // 遍历找到是否有父容器和当前父容器一致的WebView或者最早空闲的那个WebView
        for (int i = 0; i < customizeMediaViewList.size(); i++) {
            CustomizeMediaView customizeMediaView = customizeMediaViewList.get(i);
            if (customizeMediaView == null) {
                continue;
            }
            if (customizeMediaView.getParent() != null && customizeMediaView.getParent() == container) {
                return customizeMediaView;
            }
            if (customizeMediaView.isIdle() && customizeMediaView.getIdleTime() < tempIdleTime) {
                tempIdleTime = customizeMediaView.getIdleTime();
                idleCustomizeMediaView = customizeMediaView;
            }
        }
        if (idleCustomizeMediaView == null) {
            idleCustomizeMediaView = new CustomizeMediaView(container.getContext());
            customizeMediaViewList.add(idleCustomizeMediaView);
        }
        return idleCustomizeMediaView;
    }

    /**
     *
     * @param key
     */
    private synchronized void release(String key) {
        try {
            List<CustomizeMediaView> customizeMediaViewList = mediaViewViewCacheMap.get(key);
            if (customizeMediaViewList != null) {
                ADSuyiAdUtil.releaseList(customizeMediaViewList);
                mediaViewViewCacheMap.remove(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
